//
//  HMRegisteredController.m
//  HeartMark
//
//  Created by rimi on 16/11/9.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMRegisteredController.h"
#import <Masonry.h>
#import <AVOSCloud/AVOSCloud.h>
#import "HMTabBarViewController.h"

@interface HMRegisteredController ()

@property(nonatomic,strong)UITextField* userT;
@property(nonatomic,strong)UITextField* passwordT;
@property(nonatomic,strong)UITextField* repeatPasswordT;
@property(nonatomic,strong)UITextField* emailT;
@end

@implementation HMRegisteredController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //设置背景图
    UIImageView* bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgImage.image = [UIImage imageNamed:@"regies_bg.jpg"];
    [self.view addSubview:bgImage];
    [self setupUI];
}

- (void)textFBeginEdit {
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = -(SCREEN_HEIGHT * 0.21);
    self.view.frame = tempRect;
}

-(void)setupUI{

    
    //设置输入框
    _userT = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.225, SCREEN_HEIGHT * 0.25, SCREEN_WIDTH * 0.55, 36)];
    //圆角模式
    _userT.borderStyle = UITextBorderStyleRoundedRect;
    _userT.layer.masksToBounds = YES;
    _userT.placeholder = @"请输入注册账号";
    _userT.font = [UIFont systemFontOfSize:15];
    [_userT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_userT];
    
    _passwordT = [[UITextField alloc]init];
    _passwordT.placeholder = @"请输入你的密码";
    //圆角模式
    _passwordT.borderStyle = UITextBorderStyleRoundedRect;
    _passwordT.secureTextEntry = YES;//密码模式
    _passwordT.font = [UIFont systemFontOfSize:15];
    [_passwordT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_passwordT];
    [_passwordT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userT.mas_left);
        make.top.equalTo(_userT.mas_bottom).offset(SCREEN_HEIGHT * 0.022);
        make.width.equalTo(_userT.mas_width);
        make.height.equalTo(_userT.mas_height);
    }];
    
    //重复输入密码框
    _repeatPasswordT = [[UITextField alloc]init];
    _repeatPasswordT.placeholder = @"请再次输入你的密码";
    //圆角模式
    _repeatPasswordT.borderStyle = UITextBorderStyleRoundedRect;
    _repeatPasswordT.secureTextEntry = YES;//密码模式
    _repeatPasswordT.font = [UIFont systemFontOfSize:15];
    [_repeatPasswordT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_repeatPasswordT];
    [_repeatPasswordT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_userT.mas_left);
        make.top.equalTo(_passwordT.mas_bottom).offset(SCREEN_HEIGHT * 0.022);
        make.width.equalTo(_userT.mas_width);
        make.height.equalTo(_userT.mas_height);
    }];
    //邮箱输入框
    _emailT = [[UITextField alloc]init];
    _emailT.placeholder = @"请输入你的邮箱";
    //圆角模式
    _emailT.borderStyle = UITextBorderStyleRoundedRect;
     _emailT.font = [UIFont systemFontOfSize:15];
    [_emailT addTarget:self action:@selector(textFBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [self.view addSubview:_emailT];
    [_emailT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_repeatPasswordT.mas_left);
        make.top.equalTo(_repeatPasswordT.mas_bottom).offset(SCREEN_HEIGHT * 0.022);
        make.width.equalTo(_userT.mas_width);
        make.height.equalTo(_userT.mas_height);
    }];
    
    
    UIButton* registeredBtn = [[UIButton alloc] init];
    registeredBtn.layer.cornerRadius = 5;
    registeredBtn.backgroundColor = [UIColor orangeColor];
    [registeredBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registeredBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registeredBtn addTarget:self action:@selector(registeredBtnCLicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registeredBtn];
    
    [registeredBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_emailT.mas_bottom).offset(SCREEN_HEIGHT * 0.04);
        make.left.equalTo(_userT.mas_left);
        make.right.equalTo(_userT.mas_right);
        make.height.equalTo(@40);
    }];
    
    UIButton *returnLoginBtn = [[UIButton alloc] init];
    returnLoginBtn.layer.cornerRadius = 5;
    [returnLoginBtn setTitle:@"返回登录" forState:(UIControlStateNormal)];
    returnLoginBtn.backgroundColor = [UIColor orangeColor];
    [returnLoginBtn addTarget:self action:@selector(returnLoginBtn) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:returnLoginBtn];
    [returnLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registeredBtn.mas_bottom).offset(10);
        make.left.equalTo(_userT.mas_left);
        make.width.equalTo(registeredBtn.mas_width);
        make.height.equalTo(@40);
    }];

    
}

- (void)returnLoginBtn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)getToInt:(NSString*)strtemp
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* da = [strtemp dataUsingEncoding:enc];
    return [da length];
}

-(void)registeredBtnCLicked:(UIButton *)button{
    [self.view endEditing:YES];
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = 0;
    self.view.frame = tempRect;
    
    if ([_userT.text isEqualToString:@""]) {
        [self setAlertVc:@"用户名不能为空！" doGO:NO];
        return;
    }
    
    NSInteger userStrLength = [self getToInt:_userT.text];
    
    
    if (userStrLength < 3) {
        [self setAlertVc:@"用户名过短！" doGO:NO];
        return;
    } else if (userStrLength > 12) {
        [self setAlertVc:@"用户名过长！" doGO:NO];
        return;
    }
    
    
    if ([_passwordT.text isEqualToString:@""]) {
        [self setAlertVc:@"密码不能为空！" doGO:NO];
        return;
    }
    
    
    if (_passwordT.text.length < 6) {
        [self setAlertVc:@"密码长度过短！" doGO:NO];
        return;
    } else if (_passwordT.text.length > 18) {
        [self setAlertVc:@"密码长度过长！" doGO:NO];
        return;
    }
    if ([_emailT.text isEqualToString:@""]) {
        [self setAlertVc:@"请输入邮箱号码，以方便在您忘记密码的时候用以找回密码！" doGO:NO];
        return;
    }
    
    
    if ([_passwordT.text isEqualToString:_repeatPasswordT.text]) {
        
        NSString *username = self.userT.text;
        NSString *password = self.passwordT.text;
        
        if (username && password ) {
            [button setUserInteractionEnabled:NO];
            AVUser *user = [AVUser user];
            user.username = username;
            user.password = password;
            
            user.email = _emailT.text;
            
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    
                    [self.view endEditing:YES];
                    [self setAlertVc:@"注册成功,请前往邮箱激活绑定" doGO:YES];
//                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//                    [userDefaults setObject:@"NO" forKey:@"noteSaveArr"];
                    
                } else {
                    if (error.code == 202) {
                        [self setAlertVc:@"该账号已被注册,请重新输入用户名注册" doGO:NO];
                        
                    } else if (error.code == 203){
                        [self setAlertVc:@"该邮箱已经被其他账号绑定" doGO:NO];
                        
                    } else if (error.code == 125) {
                        [self setAlertVc:@"邮箱地址无效" doGO:NO];
                    }
                    else {
                        [self setAlertVc:@"网络异常，请检查网络" doGO:NO];
                        
                    }
                    
                    [button setUserInteractionEnabled:YES];
                }
            }];
            
        }
        
    }else{
        [self setAlertVc:@"两次输入密码不相同" doGO:NO];
        
    }
    
}


-(void)setAlertVc:(NSString*)string doGO:(BOOL)dogo{
    
    UIAlertController* alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (dogo) {
            HMTabBarViewController *tabBarVC = [[HMTabBarViewController alloc] init];
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            
            window.rootViewController = tabBarVC;
            
        } else {
            [alertVc dismissViewControllerAnimated:YES completion:nil];
        }
    }]];
    
    [self presentViewController:alertVc animated:YES completion:nil];
    
}
#pragma mark -退出键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    CGRect tempRect = self.view.frame;
    tempRect.origin.y = 0;
    self.view.frame = tempRect;
}
@end
